package core.cwm;

import java.util.List;

import core.access.Read;
import core.access.Right;
import core.access.Write;
import core.obj.Object;
import core.sub.Company;
import core.sub.Subject;

public class StarCondValidator implements Validator {

	@Override
	public boolean validate(Subject sub, Object obj, Right right) {
		if(right instanceof Write) {
			boolean b = ValidatorMgr.getInstance().getCWMSSCondValidator().validate(sub, obj, Read.getRight());
			if(b) {
				CompanyRecord CDs = CompanyRecord.getInstance();
				Company C = CDs.lookup(obj);
				if(C != null) {
					PRRecord PRs = PRRecord.getInstance();
					PRObject subPR = PRs.getPRObjects(sub);
					List<Object> subObjs = subPR.getObjects();
					if(subObjs != null) {
						for(int i=0;i<subObjs.size();i++) {
							Object o = subObjs.get(i);
							if(o.isCompanyData()) {
								if(!o.isSanitized() && 
									ValidatorMgr.getInstance().getCWMSSCondValidator().validate(sub, o, Read.getRight())) {
									Company C_o = CDs.lookup(o);
									if(!C.equals(C_o)) {
										return false;
									}
								}
							} else {
								// Not company data
							}

						}
						// Satisfy condition 2
						return true;
					} else {
						// subject has not read any object, not allow right
						return false;
					}
				} else {
					// obj is an object not belong to any companies 
					// Pass to other validator or raise some errors			
				}
				return false;				
			}
		}
		return false;
	}

}
